package ${PACKAGE_NAME};

#set($LOWER_NAME=${NAME.toLowerCase()})
import com.foresee.ems.utils.paging.PageCast;
import com.foresee.ems.utils.vo.PageData;
import com.foresee.ems.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * controller
 *
 * @author ${USER} on ${DATE}.
 * @version 1.0
 */
@Controller
@RequestMapping("${url}")
public class ${NAME}Controller extends BaseController {
    @Resource
    private ${NAME}Service ${LOWER_NAME}Service;

    /**
     * 表格
     *
     * @return
     */
    @RequiresPermissions("${LOWER_NAME}:list")
    @RequestMapping(value = "")
    public String list(Model model) {
        ${NAME} ${LOWER_NAME} = new ${NAME}();
        model.addAttribute("${LOWER_NAME}", ${LOWER_NAME});
        return "${url}/${LOWER_NAME}";
    }

    /**
     * 查询
     *
     * @param ${LOWER_NAME}     包含查询条件的对象
     * @param curPage  当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    @RequestMapping(value = "", params = {"action=list"})
    @ResponseBody
    public PageData list(${NAME} ${LOWER_NAME}, @RequestParam(defaultValue = "1") int curPage, @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<${NAME}> all = this.${LOWER_NAME}Service.findAll(${LOWER_NAME}, curPage, pageSize);
        return PageCast.cast2PageData(all);
    }

    /**
     * 根据id查询，供修改编辑时使用
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "", params = {"action=get"})
    @ResponseBody
    public Object updateForm(String id) {
        if (StringUtils.isNotEmpty(id)) {
            return ${LOWER_NAME}Service.findById(id);
        }
        return super.operFail();
    }

    /**
     * 修改
     *
     * @param ${LOWER_NAME}
     * @return
     */
    @RequiresPermissions(value = {"${LOWER_NAME}:add", "${LOWER_NAME}:edit"}, logical = Logical.OR)
    @RequestMapping(value = "", params = {"action=update"})
    @ResponseBody
    public Object update(@Validated ${NAME} ${LOWER_NAME}, BindingResult result) {
        if (result.hasErrors()) {
            return createOperMessage(result);
        }
        this.${LOWER_NAME}Service.save(${LOWER_NAME});
        return super.operSuccess();
    }

    /**
     * 删除
     *
     * @param ${LOWER_NAME}
     * @return
     */
    @RequiresPermissions("${LOWER_NAME}:del")
    @RequestMapping(value = "", params = {"action=delete"})
    @ResponseBody
    public Object delete(${NAME} ${LOWER_NAME}) {
        this.${LOWER_NAME}Service.delete(${LOWER_NAME});
        return super.delSuccess();
    }    
}

